package com.neusoft.utils.tpl.res;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.neusoft.utils.string.StringUtil;
import com.neusoft.utils.tpl.DirectiveUtils;
import com.neusoft.utils.tpl.bean.ResBean;
import com.neusoft.utils.tpl.help.ReadJsonToMap;
import com.neusoft.utils.web.RequestUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class CssOrgDirective implements TemplateDirectiveModel {
	public static final String PARAM_SRC = "src";
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		String src = DirectiveUtils.getString(PARAM_SRC, params);
		HttpServletRequest req =RequestUtils.getRequest();
		String base=req.getContextPath();
		Map<String, String> configMap = ReadJsonToMap.read(getFile("commonConfig/css.json"));
		String[] cssSrcs = src.split(",");
		List<ResBean> resList = new ArrayList<ResBean>();
		for (String cssStr : cssSrcs) {
			if (null == configMap.get(cssStr)) {
				boolean isCdn=isCdn(cssStr);
				long time = getFileTime(cssStr,isCdn);
				cssStr=getFileStr(cssStr,isCdn);
				resList.add(new ResBean().setPath(cssStr).setTime(time).setIsCdn(isCdn));
			} else {
				String[] cssSubSrcs = configMap.get(cssStr).split(",");
				for (String cssSubSrc : cssSubSrcs) {
					boolean isCdn=isCdn(cssSubSrc);
					long time = getFileTime(cssSubSrc,isCdn);
					cssSubSrc=getFileStr(cssSubSrc,isCdn);
					resList.add(new ResBean().setPath(cssSubSrc).setTime(time).setIsCdn(isCdn));
				}
			}
		}
		String result ="";
	   result=renderCssList(resList,base);
	    env.getOut().append(result);
	}
    private String renderCssList(List<ResBean> resList,String base ){
    	StringBuffer resultSb=new StringBuffer();
	    for (ResBean resBean : resList) {
	    	if(resBean.getIsCdn()){
	    		resultSb.append(StringUtil.format("<link rel=\"stylesheet\" href=\"{0}\">",resBean.getPath())).append("\r\n");
	    	}else{
		    	//<link rel="stylesheet" href="applySquare.css">
		    	resultSb.append(StringUtil.format("<link rel=\"stylesheet\" href=\"{0}/{1}?v={2}\">",base,resBean.getPath(),resBean.getTime().toString())).append("\r\n");			
	    	}
	   }
	    return resultSb.toString();
    }
    private long getFileTime(String path,boolean isCdn) {
		if(isCdn){
			return 1L;
		}
		return new File(RequestUtils.getWebRealPath(path)).lastModified();
	}
	private boolean isCdn(String path){
		if(path.startsWith("@")){
			return true;
		}
		return false;
	}
	private String getFileStr(String path,boolean isCdn) {
		if(isCdn){
			return path.substring(1, path.length());
		}
		return path;
	}

	private File getFile(String path) {
		return new File(RequestUtils.getWebRealPath(path));
	}

}
